!function (e, $, mui) {
    "use strict";
	
	var config = {
	};
	
    var dtPicker = new mui.DtPicker();
    var picker = new mui.PopPicker();
	// 定义页面
	var p = function(config) {
		var t = this;
		t.config = config;
		t.userInfo = null;
		t.formdata = {
			ship_company: '',
			vessel: '',
			goods_name: '',
			voyage_no: '',
			bl_no: '',
			container_type: '',
			container_no: '',
			quantity: '',
			net_weight: '',
			gross_weight: '',
			volume: ''
		};
		t.element = {
			quick_mark: $('.quick-mark'),
			ship_company: $('.ship_company'),
			vessel: $('.vessel'),
			goods_name: $('.goods_name'),
			voyage_no: $('.voyage_no'),
			bl_no: $('.bl_no'),
			container_type: $('.container_type'),
			container_no: $('.container_no'),
			quantity: $('.quantity'),
			net_weight: $('.net_weight'),
			gross_weight: $('.gross_weight'),
			volume: $('.volume'),
			submit: $('.mui-submit')
		};
		t.init();
	};
	
	var pt = p.prototype;
	    
    // 配置信息获取
    function configReady(config_type, callback) {
    	if(config[config_type]) {
    		return callback(config[config_type]);
    	} else {
    		switch(config_type) {
    			case 'ship_company':
    			case 'container_type':
    				baseApi.post({
						op: 'dictionary',
						dict_type: config_type
					}, function (result) {
						result = result.result;
						config[config_type] = [];
						result.forEach(function (item) {
							config[config_type].push({
								text: item.val,
								value: item.key
							});
						});
						return callback(config[config_type]);
					});
    			break;
    		}
    	}
    }
	
	// 页面初始化
	pt.init = function () {
		var t = this;
		t.userInit();
		// 监听
		t.listen();
	};
	
	// 用户信息初始化
	pt.userInit = function() {
		var t = this;
		t.userInfo = getLocalUserInfo();
	};
	
	// 事件监听
	pt.listen = function () {
		var t = this;
		// 调取二维码识别
		mui.plusReady(function () {
			var qm = new window.quickMark({
				callback: function (type, code) {
					console.log( code);
					t.loadQrcodeData(code);
				}
			});
			t.element.quick_mark.on('tap', function () {
				qm.scanStart();
			});
		});
		
		// 选择船公司
		t.element.ship_company.parent().on('tap', function () {
			configReady('ship_company', function (c) {
				picker.setData(c);
			    picker.show(function (item) {
			    	item = item[0];
			    	t.element.ship_company.text(item.text);
			    	t.formdata.ship_company = item.value;
			    });
			});
		});
		// 选择箱型
		t.element.container_type.parent().on('tap', function () {
			configReady('container_type', function (c) {
				picker.setData(c);
			    picker.show(function (item) {
			    	item = item[0];
			    	t.element.container_type.text(item.text);
			    	t.formdata.container_type = item.text;
			    });
			});
		});
		
		// 提交
		t.element.submit.on('tap', function () {
			var formdata = t.formdata;
			formdata.goods_name = t.element.goods_name.val().trim();
			formdata.vessel = t.element.vessel.val().trim();
			formdata.voyage_no = t.element.voyage_no.val().trim();
			formdata.bl_no = t.element.bl_no.val().trim();
			formdata.container_no = t.element.container_no.val().trim();
			formdata.quantity = t.element.quantity.val().trim();
			formdata.net_weight = t.element.net_weight.val().trim();
			formdata.gross_weight = t.element.gross_weight.val().trim();
			formdata.volume = t.element.volume.val().trim();
			if(!formdata.goods_name) {
				mui.toast('请输入品名');
				return;
			}
			if(!formdata.ship_company) {
				mui.toast('请输入船公司');
				return;
			}
			if(!formdata.vessel) {
				mui.toast('请输入船名');
				return;
			}
			if(!formdata.voyage_no) {
				mui.toast('请输入航次');
				return;
			}
			if(!formdata.bl_no) {
				mui.toast('请输入提单号');
				return;
			}
			if(!formdata.container_type) {
				mui.toast('请输入箱型');
				return;
			}
			if(!formdata.container_no) {
				mui.toast('请输入箱号');
				return;
			}
			if(!formdata.quantity) {
				mui.toast('请输入件数');
				return;
			}
			if(!formdata.net_weight) {
				mui.toast('请输入净重');
				return;
			}
			if(!formdata.gross_weight) {
				mui.toast('请输入毛重');
				return;
			}
			if(!formdata.volume) {
				mui.toast('请输入体积');
				return;
			}
			
			formdata.op = 'preEntry';
			// 提交
	    	baseApi.post(formdata, function (result) {
				mui.toast('提交成功');
				// 刷新上一页
				plus.webview.currentWebview().opener().evalJS('window.page.listInit();');
				// 关闭当前页
				plus.webview.currentWebview().close();
			});
		});
	}
	
	// 加载二维码数据
	pt.loadQrcodeData = function(url) {
		var t = this;
		url = url.replace(/"/img, '');
		$.ajax({ 
			url: url+'?type=json1',
			type: 'GET',
			dataType: 'json',
			beforeSend: function () {
				plus.nativeUI.showWaiting();
			},
			error: function (res) {
				plus.nativeUI.closeWaiting();
				console.log(JSON.stringify(res));
			},
			success: function (res) {
				plus.nativeUI.closeWaiting();
				var result = res.response.container;
				if(result.container_no) t.element.container_no.val(result.container_no);
				if(result.container_type) {
					t.formdata.container_type = result.container_type;
					t.element.container_type.text(result.container_type);
				}
				if(result.ship_company) {
					t.formdata.ship_company = result.ship_company;
					t.element.ship_company.text(result.ship_company_format);
				}
				if(result.vessel) t.element.vessel.val(result.vessel);
				if(result.goods_name) t.element.goods_name.val(result.goods_name);
				if(result.voyage_no) t.element.voyage_no.val(result.voyage_no);
				if(result.bl_no) t.element.bl_no.val(result.bl_no);
			}
		});
	};
	
	// 页面初始化
	e.page = new p(config);
	mui.plusReady(function () {
		mui.init({
			beforeback: function () {
				// 刷新上一页
				plus.webview.currentWebview().opener().evalJS('window.page.listInit();');
			}
		});
	});

}(window, $, mui);